package servlet.Jamin;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import pojo.Coupon;
import service.impl.CouponServiceImpl;
import util.JsonDateValueProcessor;
import util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(value = "/CouponServlet")
public class PageBeanCoupon extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CouponServiceImpl cs=new CouponServiceImpl();
        PrintWriter out = resp.getWriter();
        PageBean<Coupon> pb=new PageBean<Coupon>();
        String index=req.getParameter("page");
        if (index==null){
            pb.setPageIndex(1);
        }else{
            pb.setPageIndex(Integer.parseInt(index));
        }
        String pagesize=req.getParameter("limit");
        if (pagesize==null){
            pb.setPageSize(5);
        }else{
          pb.setPageSize(Integer.parseInt(pagesize));
        }
pb=cs.getCoupon(pb);
        JSONObject js=new JSONObject();
        js.put("code",0);
        js.put("msg","");
        js.put("count",pb.getTotalCount());
        JsonConfig config=new JsonConfig();
        config.registerJsonValueProcessor(java.util.Date.class,new JsonDateValueProcessor());
        JSONArray array=new JSONArray();
        js.put("data",array.fromObject(pb.getList(),config));
out.print(js);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
